<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="${packageName}.dao.${tableInfo.tableClassName}Mapper" >
  <resultMap id="BaseResultMap" type="${packageName}.entity.${tableInfo.tableClassName}" >
     <#list tableInfo.tableColumns as columnInfo>
     	<#if columnInfo.jdbcIsKey>
     	<!--  ${columnInfo.remark}-->
        <id column="${columnInfo.jdbcName}" property="${columnInfo.javaName}" jdbcType="${columnInfo.ibatisJdbcType}"/>
     	<#else>
     	<!--  ${columnInfo.remark}-->
        <result column="${columnInfo.jdbcName}" property="${columnInfo.javaName}" jdbcType="${columnInfo.ibatisJdbcType}"/>
     	</#if>
    </#list>
  </resultMap>
  
  ${r'
  <sql id="Example_Where_Clause" >
    <where >
      <foreach collection="oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  
  <sql id="Update_By_Example_Where_Clause" >
    <where >
      <foreach collection="example.oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >
          <trim prefix="(" suffix=")" prefixOverrides="and" >
            <foreach collection="criteria.criteria" item="criterion" >
              <choose >
                <when test="criterion.noValue" >
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue" >
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue" >
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue" >
                  and ${criterion.condition}
                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  '}
  
  <sql id="Base_Column_List" >
    <#list tableInfo.tableColumns as columnInfo>${columnInfo.jdbcName}<#if columnInfo_has_next>,</#if></#list>
  </sql>
 
  <!--查询列表 -->
  <select id="selectByExampleList" resultMap="BaseResultMap" parameterType="${packageName}.entity.${tableInfo.tableClassName}Example" >
    select
    <if test="distinct" >
      distinct
    </if>
    'false' as QUERYID,
    <include refid="Base_Column_List" />
    from ${tableInfo.tableName}
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
   ${r'  order by ${orderByClause} '}
    </if>
  </select>
  
  <!--分页查询列表 -->
   <select id="selectByExamplePage" resultMap="BaseResultMap" parameterType="${packageName}.entity.${tableInfo.tableClassName}Example" >
    select
    <if test="distinct" >
      distinct
    </if>
    'false' as QUERYID,
    <include refid="Base_Column_List" />
    from ${tableInfo.tableName}
    <if test="_parameter != null" >
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null" >
   ${r'  order by ${orderByClause} '}
    </if>
  </select>
  
  <!--根据id查询 -->
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" >
    select 
    <include refid="Base_Column_List" />
    from ${tableInfo.tableName}
     where ${tableInfo.tablePkInfo.jdbcName} = ${r'#{'}${tableInfo.tablePkInfo.javaName},jdbcType=${tableInfo.tablePkInfo.ibatisJdbcType}${r'}'}
  </select>
  
  <!--新增全部字段-->
  <insert id="insert" parameterType="${packageName}.entity.${tableInfo.tableClassName}" >
    insert into ${tableInfo.tableName} (
        <#list tableInfo.tableColumns as columnInfo>
     	 ${columnInfo.jdbcName}<#if columnInfo_has_next>,</#if>
        </#list>
    )
    values (
        <#list tableInfo.tableColumns as columnInfo>
        ${r'#{'}${columnInfo.javaName},jdbcType=${columnInfo.ibatisJdbcType}${r'}'}<#if columnInfo_has_next>,</#if>
        </#list>
      )
  </insert>
  
  <!--新增部分字段-->
  <insert id="insertSelective" parameterType="${packageName}.entity.${tableInfo.tableClassName}" >
    insert into ${tableInfo.tableName}
    <trim prefix="(" suffix=")" suffixOverrides="," >
        <#list tableInfo.tableColumns as columnInfo>
        	<if test="${columnInfo.javaName} != null" >
	        ${columnInfo.jdbcName}<#if columnInfo_has_next>,</#if>
	        </if>
        </#list>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
        <#list tableInfo.tableColumns as columnInfo>
	      <if test="${columnInfo.javaName} != null" >
        	${r'#{'}${columnInfo.javaName},jdbcType=${columnInfo.ibatisJdbcType}${r'}'}<#if columnInfo_has_next>,</#if>
	      </if>
        </#list>      
    </trim>
  </insert>
  
  
    <!--  批量 ，传入一个长度为1000的list  -->  
	<insert id="insertBatch" >  
	    insert into  ${tableInfo.tableName} ( <include refid="Base_Column_List_insert" /> )   
	    values   
	    <foreach collection="list" item="item" index="index" separator=",">  
	        (
	         <#list tableInfo.tableColumns as columnInfo>
	        ${r'#{'}item.${columnInfo.javaName}${r'}'}<#if columnInfo_has_next>,</#if>
	        </#list>
	      )
	    </foreach>  
	</insert>  
	
	 <sql id="Base_Column_List_insert" >
	    <#list tableInfo.tableColumns as columnInfo>
	     	 ${columnInfo.jdbcName}<#if columnInfo_has_next>,</#if>
	     </#list>
	  </sql>
  
  
  <!--更新部分字段-->
  <update id="updateByPrimaryKeySelective" parameterType="${packageName}.entity.${tableInfo.tableClassName}" >
    update ${tableInfo.tableName}
    <set >
   		 <#list tableInfo.tableColumns as columnInfo>
   		 <#if columnInfo.jdbcIsKey==false>
	      <if test="${columnInfo.javaName} != null" >
        	${columnInfo.jdbcName} = ${r'#{'}${columnInfo.javaName},jdbcType=${columnInfo.ibatisJdbcType}${r'}'}<#if columnInfo_has_next>,</#if>
	      </if>
	      </#if>
	    </#list>  
    </set>
     where ${tableInfo.tablePkInfo.jdbcName} = ${r'#{'}${tableInfo.tablePkInfo.javaName},jdbcType=${tableInfo.tablePkInfo.ibatisJdbcType}${r'}'} 
  </update>
  
  <!--更新全部字段-->
   <update id="updateByPrimaryKey" parameterType="${packageName}.entity.${tableInfo.tableClassName}" >
    update ${tableInfo.tableName}
    set 
	    <#list tableInfo.tableColumns as columnInfo>
	    <#if columnInfo.jdbcIsKey==false>
	        ${columnInfo.jdbcName} = ${r'#{'}${columnInfo.javaName},jdbcType=${columnInfo.ibatisJdbcType}${r'}'}<#if columnInfo_has_next>,</#if>
	    </#if>
		</#list> 
    
     where ${tableInfo.tablePkInfo.jdbcName} = ${r'#{'}${tableInfo.tablePkInfo.javaName},jdbcType=${tableInfo.tablePkInfo.ibatisJdbcType}${r'}'} 
  </update>
  
  
  <!--根据条件删除-->
  <delete id="deleteByExample" parameterType="${packageName}.entity.${tableInfo.tableClassName}Example">
    delete from ${tableInfo.tableName}
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  
</mapper>